<?php



function clean_var($variable) {
    $variable = strip_tags(stripslashes(trim(rtrim($variable))));
    return $variable;
}

class PageController extends FrontController
{

    public function actionIndex()
    {
        if (!isset($_GET['url']))
        {
            throw new CHttpException(404, Yii::t('frontend','The requested page does not exist.'));
        }
        $cri = new CDbCriteria();
        $cri->condition = 'url=:u';
        $cri->params = array(
            ':u' => $_GET['url']
        );
        $page = Page::model()->find($cri);

        if (!$page)
            throw new CHttpException(404, Yii::t('frontend','The requested page does not exist.'));
        $this->meta_description = $page->meta_description;
        $this->meta_keywords = $page->meta_keywords;
        $this->render('index', array(
            'page' => $page,
        ));
    }

    public function actionFaq()
    {
        $this->render('faq');
    }

    public function actionwebmasterInfo(){
        Yii::app()->theme = 'frontend-webmaster-info';
        $this->assetDir = Yii::app()->theme->baseUrl . '/assets/';
        $this->render('webmaster-info-default');
    }
    public function actionContact(){
        /**
         * Description of PageController
         *
         */
    // Who you want to recieve the emails from the form.
        $sendto = 'hepek@awerest.com';

    // The subject you'll see in your inbox
        $subject = Yii::t('frontend','A message from hepek service');

    // Message for the user when he/she doesn't fill in the form correctly.
        $errormessage = Yii::t('frontend','Looks like you are missing some info. Try again.');

    //Message for the user when he/she fills in the form correctly.
        $thanks = Yii::t('frontend','Thanks for the email. We\'ll get back to you as soon as we can.');

    // Message for the bot when it fills in in at all.
        $honeypot = Yii::t('frontend','You filled in the honeypot! If you\'re human, try again!');

    // Various messages displayed when the fields are empty.
        $emptyname =  Yii::t('frontend','Entering your name?');
        $emptyemail = Yii::t('frontend','Entering your email address?');
        $emptymessage = Yii::t('frontend','Entering a message?');

    // Various messages displayed when the fields are incorrectly formatted.
        $alertname =  Yii::t('frontend','Entering your name using only the standard alphabet?');
        $alertemail = Yii::t('frontend','Entering your email in this format: <i>name@example.com</i>?');
        $alertmessage = Yii::t('frontend','Making sure you aren\'t using any parenthesis or other escaping characters in the message? Most URLS are fine though!');

        $alert = '';
        $pass = 0;
        if ( empty($_REQUEST['last']) ) {

            if ( empty($_REQUEST['name']) ) {
                $pass = 1;
                $alert .= "<li>" . $emptyname . "</li>";
                $alert .= "<script>jQuery(\"#name\").addClass(\"error\");</script>";
            } elseif ( ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] ) ) {
                $pass = 1;
                $alert .= "<li>" . $alertname . "</li>";
            }
            if ( empty($_REQUEST['email']) ) {
                $pass = 1;
                $alert .= "<li>" . $emptyemail . "</li>";
                $alert .= "<script>jQuery(\"#email\").addClass(\"error\");</script>";
            } elseif ( !eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_REQUEST['email']) ) {
                $pass = 1;
                $alert .= "<li>" . $alertemail . "</li>";
            }
            if ( empty($_REQUEST['message']) ) {
                $pass = 1;
                $alert .= "<li>" . $emptymessage . "</li>";
                $alert .= "<script>jQuery(\"#message\").addClass(\"error\");</script>";
            } elseif ( ereg( "[][{}()*+\\^$|]", $_REQUEST['message'] ) ) {
                $pass = 1;
                $alert .= "<li>" . $alertmessage . "</li>";
            }

            if ( $pass==1 ) {

                echo "<script>$(\".message\").toggle();$(\".message\").toggle().hide(\"slow\").show(\"slow\"); </script>";
                echo "<script>$(\".alert\").addClass('alert-block alert-error').removeClass('alert-success'); </script>";
                echo $errormessage;
                echo $alert;

            } elseif (isset($_REQUEST['message'])) {

//                $message = "From: " . clean_var($_REQUEST['name']) . "\n";
//                $message .= "Email: " . clean_var($_REQUEST['email']) . "\n";
//                $message .= "Message: \n" . clean_var($_REQUEST['message']);
//                $header = 'From:'. clean_var($_REQUEST['email']);
//
//                mail($sendto, $subject, $message, $header);
                $contact = new Contact();
                $contact->email=$_REQUEST['email'];
                $contact->first_name=$_REQUEST['name'];
                $contact->message=$_REQUEST['message'];
                if ($contact->validate() && $contact->save(false))
                {
                    try
                    {
                        MyEmail::send_new_contact_to_admin($contact);
                        MyEmail::send_new_contact_to_user($contact);
                    }
                    catch (Exception $ex)
                    {
                        MyEmail::send_error('can not send email contact Sitecontroller.php line:292');
                    }

                    //Yii::app()->user->setFlash('contact_sucessfully', 'Thank you for contacting us. We will respond to you as soon as possible.');

                    //Yii::app()->request->redirect(Yii::app()->createUrl('frontend/site/contact'));
                   // Yii::app()->end();
                }
                else
                {
                    MyEmail::send_error('can not save contact Sitecontroller.php line:230');
                    #Yii::app()->end();
                    //Yii::app()->user->setFlash('contact_error', 'Can not send email');
                }


                echo "<script>$(\".message\").toggle();$(\".message\").toggle().hide(\"slow\").show(\"slow\");$('#contactForm')[0].reset();</script>";
                echo "<script>$(\".alert\").addClass('alert-block alert-success').removeClass('alert-error'); </script>";
                echo $thanks;
                echo "<script>jQuery(\"#name\").removeClass(\"error\");jQuery(\"#email\").removeClass(\"error\");jQuery(\"#message\").removeClass(\"error\");</script>";
                Yii::app()->end();

                //echo "<br/><br/>" . $message;

            }

        } else {
            echo "<script>$(\".message\").toggle();$(\".message\").toggle().hide(\"slow\").show(\"slow\"); </script>";
            echo $honeypot;
            Yii::app()->end();
        }
    }


}

/**
 * End of PageController
 */